package cn.ym.order.controller;

import cn.ym.basic.exception.BusinessException;
import cn.ym.order.service.IOrderProductService;
import cn.ym.order.domain.OrderProduct;
import cn.ym.order.query.OrderProductQuery;
import cn.ym.basic.util.AjaxResult;
import cn.ym.basic.util.PageList;
import cn.ym.pet.domain.SearchMasterMsg;
import cn.ym.pet.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/productOrder")
public class OrderProductController {

    @Autowired
    public IOrderProductService orderProductService;

    /**
     * 接口：添加或修改
     * @param orderProduct  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                orderProductService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderProduct findOne(@PathVariable("id")Long id) {
        return orderProductService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderProduct> findAll(){
    return orderProductService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderProduct> queryPage(@RequestBody OrderProductQuery query) {
        return orderProductService.queryPage(query);
    }

    /**
     * 下订单
     * @param
     * @return
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,String> map, HttpServletRequest request){
        try {
            orderProductService.submit(map,request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }


    /**
     * 确认订单
     * @param
     * @return
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderDto orderDto){
        try {
            orderProductService.confirm(orderDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"确认订单失败");
        }
    }

    /**
     * 订单取消
     * @param id
     * @return
     */
    @GetMapping(value="/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id){
        try {
            orderProductService.cancel(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"订单取消失败");
        }
    }
}
